<?php
/**
 * SystemController is an instance of Controller (application.components). 
 *
 * Controller is an instance of CController (system.web).
 * CController manages a set of actions which deal with the corresponding user requests. 
 * Through the actions, CController coordinates the data flow between models and views. 
 *
 * Eduow :  International system for education (http://eduow.com)
 * Copyright 2010, Nikolay Stoitsev 
 *
 * Licensed under GNU General Public License version 2
 * Redistributions of files must retain the above copyright notice.
 *
 * @filesource
 * @copyright     Copyright 2010
 * @link          http://eduow.com Eduow Project
 * @package       eduow
 * @subpackage    application.controllers
 * @inheritance   class SystemController » Controller » CController » CBaseController » CComponent
 * @implements    
 * @since         Eduow v 0.0.1
 * @license       http://www.gnu.org/licenses/gpl-2.0.html GNU General Public License version 2
 */

class SystemController extends Controller
{
    /**
     * @var CActiveRecord the currently loaded data model instance.
     */
    private $_model;

    /**
     * @return array action filters
     */
    public function filters()
    {
        return array(
            'accessControl', // perform access control for CRUD operations
        );
    }

    /**
     * Specifies the access control rules.
     * This method is used by the 'accessControl' filter.
     * @return array access control rules
     */
    public function accessRules()
    {
        return array(
            '*'=>array(Group::USER, 'min'),
        );
    }

    /**
     * Save and remove user favorite posts. 
     * Browsed send AJAX request and the system check if it's value is in array 
     * with favorite articles. If it is in it, the id of the article is removed
     * from the array. If it isn't in it, the id is added in the array.
     */
    public function actionLike()
    {
        $model=$this->loadModel();
        if(isset($_GET['id']))
        {
            
            if(! is_array( $posts = json_decode($model->favorite) ) ){
	    	$posts = array();
	    }
	    
            if(in_array($_GET['id'],$posts)){

		foreach ($posts as $key => $value){
			if ($posts[$key] == $_GET['id']){
				unset($posts[$key]);
			}
		}

                if(empty($posts)){
                    $model->favorite = "";
                    $model->save();
                }else{
		   $posts=array_values($posts);
                   $model->favorite=json_encode($posts);
                   $model->save();
                }
            }else{
                 $posts[]=$_GET['id'];
		 $posts=array_values($posts);
                 $model->favorite=json_encode($posts);
                 $model->save();
            }

        }
    }

    /**
     * Save and remove user's friends. 
     * Browsed send AJAX request and the system check if it's value is in array 
     * with friend's ids. If it is in it, the id of the friend is removed
     * from the array. If it isn't in it, the id is added in the array.
     */
    public function actionFriend()
    {
        $model=$this->loadModel();
        if(isset($_GET['id']))
        {
            
            if(! is_array( $posts = json_decode($model->friends) ) ){
	    	$posts = array();
	    }
	    
            if(in_array($_GET['id'],$posts)){

		foreach ($posts as $key => $value){
			if ($posts[$key] == $_GET['id']){
				unset($posts[$key]);
			}
		}

                if(empty($posts)){
                    $model->friends = "";
                    $model->save();
                }else{
		   $posts=array_values($posts);
                   $model->friends=json_encode($posts);
                   $model->save();
                }
            }else{
                 $posts[]=$_GET['id'];
		 $posts=array_values($posts);
                 $model->friends=json_encode($posts);
                 $model->save();
            }

        }
    }

    /**
     * Save and remove user's programs. 
     * Browsed send AJAX request and the system check if it's value is in array 
     * with friend's ids. If it is in it, the id of the friend is removed
     * from the array. If it isn't in it, the id is added in the array.
     */
    public function actionPrograms()
    {
        $model=$this->loadModel();
        if(isset($_GET['id']))
        {
            
            if(! is_array( $posts = json_decode($model->programs) ) ){
	    	$posts = array();
	    }
	    
            if(in_array($_GET['id'],$posts)){

		foreach ($posts as $key => $value){
			if ($posts[$key] == $_GET['id']){
				unset($posts[$key]);
			}
		}

                if(empty($posts)){
                    $model->programs = "";
                    $model->save();
                }else{
		   $posts=array_values($posts);
                   $model->programs=json_encode($posts);
                   $model->save();
                }
            }else{
                 $posts[]=$_GET['id'];
		 $posts=array_values($posts);
                 $model->programs=json_encode($posts);
                 $model->save();
            }

        }
    }

    /**
     * Returns the data model based on the primary key given in the GET variable.
     * If the data model is not found, an HTTP exception will be raised.
     */
    private function loadModel()
    {
        if($this->_model===null)
        {
            $criteria ='user='.Yii::app()->user->id;
            $this->_model=Userdata::model()->find($criteria);

            if($this->_model===null){
                $this->_model=new Userdata;
                $this->_model->user=Yii::app()->user->id;
                $this->_model->save();
            }
        }
        return $this->_model;
    }
}
